package editor.widgets;

import editor.Images;
import util.StringUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import widgets.ImageButton;
import widgets.CustomButton;
import widgets.CustomDialog;

/**
 * This class is for getting text input from the user.
 *
 * @author Neil Dickson
 */
public class TextAreaDialog extends CustomDialog implements ActionListener {
	private Image			image;
	private	String[]		text;
	private JTextArea		textArea;
	private	ImageButton[]	buttons;
	private	int				selected;

	public static final int		NONE_SELECTED	= -1;
	public static final int		OK				= 0;
	public static final int		CANCEL			= 1;

	public static final int[]	BUTTONS_OK			= new int[] {OK};
	public static final int[]	BUTTONS_OK_CANCEL	= new int[] {OK, CANCEL};

	public TextAreaDialog(Container rootParent,Image image,String text) {
		this(rootParent,image,text,BUTTONS_OK_CANCEL);
	}
	public TextAreaDialog(Container rootParent,Image image,String text,int[] buttonTypes) {
		super(rootParent);
		this.image = image;
		this.text = StringUtil.getLines(text);
		textArea = new JTextArea(8,60);
		textArea.setFont(FONT);
		textArea.setBorder(BorderFactory.createLineBorder(FONT_COLOUR));
		textArea.setSize(480,192);
		textArea.setAutoscrolls(true);
		selected = NONE_SELECTED;

		buttons = new ImageButton[buttonTypes.length];
		for (int i=0;i<buttons.length;++i) {
			if (buttonTypes[i]==OK) {
				buttons[i] = new ImageButton(Images.OK_UP,Images.OK_HOVER,Images.OK_DOWN,Images.OK_DISABLED);
			}
			else if (buttonTypes[i]==CANCEL) {
				buttons[i] = new ImageButton(Images.CANCEL_UP,Images.CANCEL_HOVER,Images.CANCEL_DOWN,Images.CANCEL_DISABLED);
			}
			buttons[i].addActionListener(this);
		}

		updateSize();

		add(textArea);
		for (CustomButton button : buttons) {
			add(button);
		}
	}

	public void actionPerformed(ActionEvent e) {
		for (int i=0;i<buttons.length;++i) {
			if (e.getSource()==buttons[i]) {
				setVisible(false);
				selected = i;
				break;
			}
		}
	}

	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D)g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

		int textStartY = verticalSpace;
		if (TextAreaDialog.this.image!=null) {
			g2.drawImage(TextAreaDialog.this.image,(getWidth()- TextAreaDialog.this.image.getWidth(null))>>>1, verticalSpace,null);
			textStartY += TextAreaDialog.this.image.getHeight(null)+ verticalSpace;
		}

		g2.setFont(FONT);
		g2.setColor(FONT_COLOUR);
		textStartY += LINE_HEIGHT-FONT.getSize()/4-1;
		for (String line : TextAreaDialog.this.text) {
			g2.drawString(line, horizontalSpace,textStartY);
			textStartY += LINE_HEIGHT;
		}
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
	}

	public int getSelected() {
		return selected;
	}

	public void setText(String text) {
		this.text = StringUtil.getLines(text);
		updateSize();
	}

	public String getInputText() {
		return textArea.getText();
	}

	private void updateSize() {
		FontMetrics metrics = getFontMetrics(FONT);
		int width = 0;
		int height = 0;
		for (String line : text) {
			width = Math.max(width,metrics.stringWidth(line));
			height += LINE_HEIGHT;
		}

		if (image!=null) {
			width = Math.max(width,image.getWidth(null) + radius);
			height += image.getHeight(null) + verticalSpace;
		}

		textArea.setLocation(horizontalSpace,verticalSpace + height);
		width = Math.max(width,textArea.getWidth());
		height += textArea.getHeight() + verticalSpace;

		int buttonsWidth = 0;
		int buttonsHeight = 0;
		for (ImageButton button : buttons) {
			buttonsWidth += button.getWidth();
			buttonsHeight = Math.max(buttonsHeight, button.getHeight());
		}
		buttonsWidth += (buttons.length-1)* horizontalSpace;

		width = Math.max(width,buttonsWidth);
		height += buttonsHeight+ verticalSpace;

		width += horizontalSpace + horizontalSpace + shadowSpacing;
		height += verticalSpace + verticalSpace + shadowSpacing;

		setSize(width,height);

		int buttonsXOffset = 0;
		for (ImageButton button : buttons) {
			button.setLocation(((width - buttonsWidth)>>>1)+buttonsXOffset,height- shadowSpacing - verticalSpace -button.getHeight());
			buttonsXOffset += button.getWidth() + horizontalSpace;
		}

		updatePopupLocationMiddle();
	}

	public boolean interceptsKeyEvents() {
		return !textArea.hasFocus();
	}
}
